package dome2;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class Test {

	public static void main(String[] args) throws Exception {

		String resource = "dome2/mybatis.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession(false);
			InSubjectDao mapper = session.getMapper(InSubjectDao.class);
//			List<Subject> list = mapper.selectall();
//			for (Subject s : list) {
//				System.out.println(s);
//			}

//			System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			// 增加
//		SqlSession session2 = sqlSessionFactory.openSession(false);
//		Subject s = new Subject("生物与科学", 24, 5);
//		InSubjectDao mapper = session2.getMapper(InSubjectDao.class);
//		int r = mapper.add(s);
//		System.out.println(r > 0 ? "保存成功" : "保存失败");
//		session2.commit();

			// 删除
//		SqlSession session3 = sqlSessionFactory.openSession(false);
//		InSubjectDao mapper2 = session3.getMapper(InSubjectDao.class);
//		int r2 = mapper2.delete(58);
//		System.out.println("删除了" + r2 + "条数据");
//		session3.commit();

			// 修改
//		SqlSession session2 = sqlSessionFactory.openSession(false);
//		Subject s = new Subject(59, "学习java基础语法", 24, 5);
//		InSubjectDao mapper = session2.getMapper(InSubjectDao.class);
//		int r = mapper.update(s);
//		System.out.println(r > 0 ? "修改成功" : "修改失败");
//		session2.commit();

			// 分页查询
//			SqlSession session2 = sqlSessionFactory.openSession(false);
//			InSubjectDao mapper2 = session2.getMapper(InSubjectDao.class);
//			Map map = new HashMap();
//			map.put("start", 1);
//			map.put("end", 5);
//			List<Subject> list = mapper.select(map);
//			System.out.println(list);
//			session.commit();
//			for (Subject s1 : list) {
//				System.out.println(s1);
//			}
			List<Subject> list = mapper.selectmohu();
			session.commit();
			for (Subject s : list) {
				System.out.println(s);
			}

		} finally {
			session.close();
		}
	}
}
